'''
This file should be runnable to print map_statistics using 
$ python stats.py
'''
from __future__ import division, print_function
from collections import namedtuple
from collections import Counter
from ways import load_map_from_csv
import sys

# Q7:
def map_statistics(roads):
    '''return a dictionary containing the desired information
    You can edit this function as you wish'''
    Stat = namedtuple('Stat', ['max', 'min', 'avg'])
    junctionsNumber = 0
    linksNumber = 0
    maxBranchingFactor = 0
    minBranchingFactor = sys.maxint
    sumOfBranchingFactors = 0
    maxLinkDistance = 0
    minLinkDistance = sys.maxint
    sumOfLinkDistance = 0
    linkTypeHistogram = Counter()
    for key, value in roads.items():
        junctionsNumber+=1
        linksNumber+= len(value.links)
        maxBranchingFactor = max(maxBranchingFactor, len(value.links))
        minBranchingFactor = min(minBranchingFactor, len(value.links))
        sumOfBranchingFactors+=len(value.links)
        for link in value.links:
            maxLinkDistance = max(maxLinkDistance, link.distance)
            minLinkDistance = min(minLinkDistance, link.distance)
            sumOfLinkDistance+= link.distance
            linkTypeHistogram[link.highway_type]+= 1
# TODO: average should be int? is the division done correctly? 
    return {
        'Number of junctions' : junctionsNumber,
        'Number of links' : linksNumber,
        'Outgoing branching factor' : Stat(max=maxBranchingFactor, min=minBranchingFactor, avg=sumOfBranchingFactors/junctionsNumber),
        'Link distance' : Stat(max=maxLinkDistance, min=minLinkDistance, avg=sumOfLinkDistance/linksNumber),
        # value should be a dictionary
        # mapping each road_info.TYPE to the no' of links of this type
        'Link type histogram' : linkTypeHistogram,  # tip: use collections.Counter
    }


def print_stats():
    for k, v in map_statistics(load_map_from_csv('israel.csv',0,10)).items():
        print('{}: {}'.format(k, v))

        
if __name__ == '__main__':
    from sys import argv
    assert len(argv) == 1
    print_stats()
